<?php
global_config::get_instance()->add_server_class("SoapFrequencyList","soap_frequency_list");
class soap_frequency_list
{
	public $Elements = array();

	public function save_to_db() {
		$dba = db_adapter::get_instance();
		 
		$values = (array)$this->Elements;
		$values = array_values($values);
		$values = is_array($values[0])? current($values) : $values;
		$result = array();
		foreach($values as $element){
			$temp = $element;
			$interval = new intervals();
			$interval->interval_length->set_value($temp->Key);
			$dba->save_unique($interval);

			$interval_count = new interval_count();
			$interval_count->chain_id->set_value($this->chain_id);
			$interval_count->interval_id->set_value($interval->interval_id->get_value());
			$interval_count->interval_type->set_value($this->type);
			$interval_count->count->set_value($temp->Value);
			$dba->save_unique($interval_count);

			$result[] = $interval_count->interval_count_id->get_value();
		}
		return $result;
	}

	public function load_from_db($chain_id, $type)
	{
		$dba = db_adapter::get_instance();
		$interval_composition = new  interval_count_intervals_composition();
		$criteria = new query_criteria();
		$criteria->add_criterion_composite($interval_composition->interval_count,$interval_composition->interval_count->chain_id,criterion_type::$equal,$chain_id);
		$criteria->add_criterion_composite($interval_composition->interval_count,$interval_composition->interval_count->interval_type,criterion_type::$equal,$type);
		$interval_composition_list = $dba->get_composite_list($interval_composition,$criteria);

		$this->Elements = new soap_array_of_frequency_list_item();
		while ($interval_count = $interval_composition_list->get_next(false, true))
		{
			$temp  = new soap_frequency_list_item();
			$temp->Key = $interval_count->intervals->interval_length->get_value();
			$temp->Value = $interval_count->interval_count->count->get_value();
			$this->Elements->add($temp);
		}
	}
}
?>